<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of produtos
 *
 * @author Lucas Pinheiro
 */
class Login extends CI_Controller {

    //put your code here
    public function __construct() {
        parent::__construct();
    }

    public function index($error = null) {
        if (!$this->session->userdata('id_usuarios')) {
            $v_dados = array();
            $v_dados['base_url'] = base_url();
            $v_dados['admin_url'] = admin_url();
            $v_dados['error'] = $error;
            $this->parser->parse('admin/login_view', $v_dados);
        } else {
            redirect(admin_url('home/index'));
        }
    }

    public function logar() {
        $this->form_validation->set_rules('email', 'E-mail', 'trim|required');
        $this->form_validation->set_rules('senha', 'Senha', 'trim|required');

        if ($this->form_validation->run() === FALSE) {
            $this->index(validation_errors());
        } else {
            $this->load->model('usuarios_model');
            $b_consultar = $this->usuarios_model->logar($this->input->post('email'), $this->input->post('senha'));
            if ($b_consultar != null) {
                $this->session->set_userdata($b_consultar);
                redirect(admin_url('home/index'));
            } else {
                $this->index('Usuário e/ou senha invalidos');
            }
        }
    }

    public function logout() {
        $this->session->sess_destroy();
        redirect(admin_url('login/index'));
    }

}